package com.duobaoyu.dby.supply.biz.client.boundary.message;

import com.duobaoyu.dby.channel.adapter.bean.message.ChannelMessage;
import com.duobaoyu.dby.channel.adapter.service.AbstractChannelMessageHandler;
import com.duobaoyu.dby.supply.biz.client.boundary.message.bean.pojo.MessagePayload;
import com.duobaoyu.dby.supply.core.framework.util.FrameworkUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName SupplyChainChannelMessageHandler
 * @Description 供应链渠道消息处理器
 * @createTime 15:39:00 2023/09/27
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class SupplyChainChannelMessageHandler extends AbstractChannelMessageHandler {

	//private final IdGeneratorSnowflake idGeneratorSnowflake;

	@Override
	protected boolean bizMessageHandler(List<ChannelMessage<Object>> channelMessages, Boolean isBatch) {
		if (CollectionUtils.isEmpty(channelMessages)) {
			log.warn("供应链渠道消息处理器生产消息为空");
			return true;
		}
		Map<String, List<ChannelMessage<Object>>> messageGroupMap = channelMessages.stream().collect(Collectors.groupingBy(ChannelMessage::getMessageLabel));
		Boolean result = null;
		for (Map.Entry<String, List<ChannelMessage<Object>>> stringListEntry : messageGroupMap.entrySet()) {
			String messageLabel = stringListEntry.getKey();
			AbstractSupplyMessageHandlerStrategy handlerStrategy = FrameworkUtils.strategyInstance(AbstractSupplyMessageHandlerStrategy.class, messageLabel);
			if (Objects.isNull(result) || result) {
				Collection<com.duobaoyu.dby.supply.db.entity.message.ChannelMessage> messages = handlerStrategy.strategyHandler(this.toMessagePayload(stringListEntry.getValue(), isBatch));
				result = Objects.nonNull(messages);
			} else {
				handlerStrategy.strategyHandler(this.toMessagePayload(stringListEntry.getValue(), isBatch));
			}
		}
		return Optional.ofNullable(result).orElse(false);
	}

	private MessagePayload toMessagePayload(List<ChannelMessage<Object>> channelMessages, Boolean isBatch) {
		return MessagePayload
				.builder()
				.isBatch(isBatch)
				.channelMessages(channelMessages.stream()
						.filter(Objects::nonNull)
						.map(message -> MessagePayload.ChannelMessage
								.builder()
								//.messageId(idGeneratorSnowflake.snowflakeId())
								.channelCode(message.getChannelCode())
								.configUniqueKey(message.getConfigUniqueKey())
								.originalMessage(message.getOriginalMessage())
								.uniqueKey(message.getUniqueKey())
								.messageType(message.getMessageType())
								.sourceClass(message.getSourceClass())
								.operationalCode(message.getOperationalCode())
								.channelOperationalCode(message.getChannelOperationalCode())
								.isBatch(message.getIsBatch())
								.data(message.getData())
								.build())
						.collect(Collectors.toList()))
				.build();
	}
}
